﻿using KingRoad.EPM.CompletionMaterialsBLL.UIRules;
using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.Web.Areas.CompletionMaterials.CompletionMaterialsFramework.Common;
using KingRoad.EPM.Web.Areas.CompletionMaterials.ViewModels;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KingRoad.EPM.Web.Areas.CompletionMaterials.Controllers
{
    public class FileListController : BaseController
    {

        /// <summary>
        /// 竣工资料—案卷目录资料管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string type)
        {
            ViewBag.Type = type;
            return View();
        }

        #region 工程项目与案卷类型管理
        /// <summary>
        /// 工程信息及材料类型信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="proId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult AsyncFileDataJson(string type, string proId, string id)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            if (proId == null)
            {
                string sql = " Pr_ID=" + PRJID + " and ";
                switch (type)
                {
                    case "1":
                        sql += " P_UType='项目法人' ";
                        break;
                    case "2":
                        sql += " P_UType='设计单位' ";
                        break;
                    case "3":
                        sql += " P_UType='施工单位' and P_Section in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
                        break;
                    case "4":
                        sql += " P_UType='监理单位' and P_Section in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
                        break;
                    case "5":
                        sql += " P_UType='相关单位' ";
                        break;
                    default:
                        sql += " 1=1 ";
                        break;
                }
                List<BcdProjectInfo> pList = BcdProjectUIRule.GetEntityListBySql(sql);
                foreach (BcdProjectInfo pInfo in pList)
                {
                    treeNodes pnode = new treeNodes();
                    pnode.proId = pInfo.P_ID;
                    pnode.pId = "null";
                    pnode.id = pInfo.P_ID + pInfo.Fd_ID;
                    pnode.open = "true";
                    pnode.name = pInfo.P_Section + pInfo.Fd_Name;
                    pnode.title = pInfo.P_Section + pInfo.Fd_Name;
                    //pnode.target = "ajax";
                    //pnode.url = "/CompletionMaterials/FileList/Detail?type=" + type + "&proId=" + pInfo.P_ID + "&id=" + pInfo.P_ID + pInfo.Fd_ID;
                    treenodes.Add(pnode);

                    List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + pInfo.P_ID + "' and Fd_PID='" + pInfo.Fd_ID + "' ");
                    foreach (BcdProjectfileInfo info in pfList)
                    {
                        treeNodes node = new treeNodes();
                        node.proId = pInfo.P_ID;
                        node.pId = pInfo.P_ID + info.Fd_PID;
                        node.id = pInfo.P_ID + info.Fd_ID;
                        node.name = info.Fd_Numb + " " + info.Fd_Name;
                        node.title=info.Fd_Numb + " " + info.Fd_Name;
                        //node.target = "ajax";
                        //node.url = "/CompletionMaterials/FileList/Detail?type=" + type + "&proId=" + pInfo.P_ID + "&id=" + pInfo.P_ID + info.Fd_ID;
                        int count = BcdProjectfileUIRule.GetCountByPIDAndFdID(pInfo.P_ID, info.Fd_ID);
                        if (count > 0)
                            node.isParent = true;
                        else
                        {
                            node.name = GetNodeName(node.name, pInfo.P_ID, info.Fd_ID);
                        }
                        treenodes.Add(node);
                    }
                }
            }
            else
            {
                id = id.Replace(proId, "");
                List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_PID='" + id + "' ");
                foreach (BcdProjectfileInfo info in pfList)
                {
                    treeNodes node = new treeNodes();
                    node.proId = proId;
                    node.pId = proId + info.Fd_PID;
                    node.id = proId + info.Fd_ID;
                    node.name = info.Fd_Numb + " " + info.Fd_Name;
                    node.title = info.Fd_Numb + " " + info.Fd_Name;
                    //node.target = "ajax";
                    //node.url = "/CompletionMaterials/FileList/Detail?type=" + type + "&proId=" + proId + "&id=" + proId + info.Fd_ID;
                    int count = BcdProjectfileUIRule.GetCountByPIDAndFdID(proId, info.Fd_ID);
                    if (count > 0)
                        node.isParent = true;
                    else
                    {
                        node.name = GetNodeName(node.name, proId, info.Fd_ID);
                    }
                    treenodes.Add(node);
                }
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }

        public string GetNodeName(string name, string pid, string fdid)
        {
            //未归档文件
            int counta = BcdVolumedirectoryUIRule.GetCountBySql(" P_ID='" + pid + "' and Fd_ID='" + fdid + "' ");
            //未归档文件（已审核通过）
            int count1 = BcdVolumedirectoryUIRule.GetCountBySql(" P_ID='" + pid + "' and Fd_ID='" + fdid + "' and Vd_State!=0 ");
            //未归档文件（已归档）
            int count2 = BcdVolumedirectoryUIRule.GetCountBySql(" P_ID='" + pid + "' and Fd_ID='" + fdid + "' and Vd_State=2 ");
            ////案卷
            //int fcount = BcdFilelistUIRule.GetCountBySql(" P_ID='" + proId + "' and (Fd_ID='" + info.Fd_ID + "') ");
            name = "(<span class='span1'>" + count2 + "</span>" + "/" + "<span class='span2'>" + count1 + "</span>" + "/" + "<span class='span3'>" + counta + "</span>" + ")" + name;
            return name;
        }

        /// <summary>
        /// 标段信息弹出框
        /// </summary>
        /// <returns></returns>
        public ActionResult SectionUnit()
        {
            return View();
        }

        #region 归档材料根类型选择
        /// <summary>
        /// 归档材料根类型选择弹出框
        /// </summary>
        /// <returns></returns>
        public ActionResult FileType()
        {
            return View();
        }
        /// <summary>
        /// 归档材料根类型Json数据
        /// </summary>
        /// <param name="utype">单位类型</param>
        /// <returns></returns>
        public JsonResult FileTypeJson(string utype)
        {
            string sql = " Fd_PID is null and Fd_UType='" + utype + "'";
            List<treeNodes> treenodes = new List<treeNodes>();
            List<BcdFiledataInfo> fList = BcdFiledataUIRule.GetEntityListBySql(sql);
            foreach (BcdFiledataInfo info in fList)
            {
                treeNodes node = new treeNodes();
                node.id = info.Fd_ID;
                node.pId = info.Fd_PID;
                node.name = info.Fd_Name;
                if (utype != "项目法人")
                    node.@checked = true;
                treenodes.Add(node);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加
        /// <summary>
        /// 归档材料表添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create(string type)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            vm.TheBcdProjectInfo = new BcdProjectInfo();
            vm.TheBcdProjectInfo.P_ID = Guid.NewGuid().ToString().ToUpper();
            switch (type)
            {
                case "1":
                    vm.TheBcdProjectInfo.P_UType = "项目法人";
                    vm.TheBcdProjectInfo.P_Unit = PRJUName;
                    break;
                case "2":
                    vm.TheBcdProjectInfo.P_UType = "设计单位";
                    vm.TheBcdProjectInfo.P_Unit = "河南省交通规划设计研究院股份有限公司";
                    vm.TheBcdProjectInfo.Fd_ID = "B900A8E3-3608-44DD-9516-D6C6D97C3F2B";
                    vm.TheBcdProjectInfo.Fd_Name = "设计审批文件";
                    break;
                case "3":
                    vm.TheBcdProjectInfo.P_UType = "施工单位";
                    vm.TheBcdProjectInfo.Fd_ID = "5AB7A838-3723-47C3-8E47-E8D077F609FE";
                    vm.TheBcdProjectInfo.Fd_Name = "施工文件";
                    break;
                case "4":
                    vm.TheBcdProjectInfo.P_UType = "监理单位";
                    vm.TheBcdProjectInfo.Fd_ID = "6E716709-A6F5-47F4-8A28-56B70006F0E3";
                    vm.TheBcdProjectInfo.Fd_Name = "监理文件";
                    break;
                case "5":
                    vm.TheBcdProjectInfo.P_UType = "相关单位";
                    vm.TheBcdProjectInfo.Fd_ID = "F026510A-48AF-4EF3-B26C-F9A5D0CB9EE3";
                    vm.TheBcdProjectInfo.Fd_Name = "科研文件";
                    break;
                default:
                    break;
            }
            vm.TheBcdProjectInfo.Pr_ID = PRJID;
            vm.TheBcdProjectInfo.P_Name = PRJName;
            vm.TheBcdProjectInfo.P_Code = PRJCode;
            return View(vm);
        }

        /// <summary>
        /// 添加页面提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreatePost(BcdFileListViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheBcdProjectInfo.P_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdProjectInfo.P_OperateUserName = GetLoginUserRealName;
                vm.TheBcdProjectInfo.P_OperateTime = DateTime.Now;
                BcdProjectUIRule.InsertEntity(vm.TheBcdProjectInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "add",
                    proId = vm.TheBcdProjectInfo.P_ID,
                    pId = "null",
                    id = vm.TheBcdProjectInfo.P_ID + vm.TheBcdProjectInfo.Fd_ID,
                    name = vm.TheBcdProjectInfo.P_Section + vm.TheBcdProjectInfo.Fd_Name,
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 归档材料类型选择
        /// <summary>
        /// 归档材料类型选择弹出框
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="pId">类型父id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public ActionResult Select(string proId, string id)
        {
            id = id.Replace(proId, "");
            ViewBag.proId = proId;
            ViewBag.id = id;
            //清除临时表中信息
            BcdProjectfiletempUIRule.DeleteByPID(proId);
            //添加临时表
            List<BcdProjectfileInfo> pflsit = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and  Fd_PIds like '%" + id + "%' ");
            foreach (BcdProjectfileInfo info in pflsit)
            {
                BcdProjectfiletempInfo pftInfo = new BcdProjectfiletempInfo();
                pftInfo.P_ID = proId;
                pftInfo.Fd_ID = info.Fd_ID;
                pftInfo.P_Fd_State = true;
                BcdProjectfiletempUIRule.InsertEntity(pftInfo);
            }
            return View();
        }

        /// <summary>
        /// 异步加载归档材料类型Json数据
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public JsonResult AsyncSelectJson(string proId, string id)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            List<BcdFiledataInfo> fList = BcdFiledataUIRule.GetEntityListBySql(" Fd_PID='" + id + "' ");
            foreach (BcdFiledataInfo info in fList)
            {
                treeNodes node = new treeNodes();
                node.proId = proId;
                node.id = info.Fd_ID;
                node.pId = info.Fd_PID;
                node.name = info.Fd_Numb + " " + info.Fd_Name;
                //读取 工程项目与材料类型关系表
                List<BcdProjectfileInfo> pflist = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and  Fd_ID='" + info.Fd_ID + "' ");
                if (pflist.Count > 0)
                {
                    node.@checked = true;
                }
                //读取 工程项目与材料类型关系表（临时表在修改时用，平时数据为空）
                List<BcdProjectfiletempInfo> pftlist = BcdProjectfiletempUIRule.GetEntityListBySql(" P_ID='" + proId + "' and  Fd_ID='" + info.Fd_ID + "' ");
                if (pftlist.Count > 0)
                {
                    BcdProjectfiletempInfo pftInfo = pftlist[0];
                    if (pftInfo.P_Fd_State == false)
                        node.@checked = false;
                    else
                        node.@checked = true;
                }
                if (BcdFiledataUIRule.GetCountByFId(info.Fd_ID) > 0)
                    node.isParent = true;
                treenodes.Add(node);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 在临时表中 勾选材料类型
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="pId">弹出框的父类型</param>
        /// <param name="id">本次勾选的类型id</param>
        public JsonResult TempSelect(string proId, string pId, string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (id != null && id != "null")
                {
                    pId = pId.Replace(proId, "");
                    id = id.Replace(proId, "");
                    BcdProjectfiletempUIRule.AddTemp(proId, pId, id);
                }
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "选择成功",
                    type = "delete",
                    proId = proId,
                    pId = "",
                    id = proId + id,
                    name = "",
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "选择失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 在临时表中 排除材料类型
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult TempDelete(string proId, string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (id != null && id != "null")
                    id = id.Replace(proId, "");
                BcdProjectfiletempUIRule.DeleteTemp(proId, id);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "排除成功",
                    type = "delete",
                    proId = proId,
                    pId = "",
                    id = proId + id,
                    name = "",
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "排除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 给项目添加勾选的材料类型
        /// </summary>
        /// <param name="type">添加类型（sel=选择，cancel=取消）</param>
        /// <param name="proId">项目id</param>
        public JsonResult SelectPost(string type, string proId, string id)
        {
            bool isParent = false;
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (type == "sel")
                {
                    BcdProjectfiletempUIRule.SelSave(proId);
                    List<BcdProjectfileInfo> spfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_PID='" + id + "' ");
                    if (spfList.Count > 0)
                        isParent = true;
                }
                if (type == "cancel")
                    BcdProjectfiletempUIRule.DeleteByPID(proId);
            });
            dynamic jsonReturn = new ExpandoObject();
            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "",
                    type = type,
                    proId = proId,
                    isParent = isParent,
                    pId = "",
                    id = proId + id,
                    name = "",
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "操作失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        #endregion

        #region 修改
        /// <summary>
        /// 归档材料表修改页面
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="pId">类型父id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public ActionResult Edit(string proId)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            vm.TheBcdProjectInfo = BcdProjectUIRule.GetEntityById(new Guid(proId));
            return View(vm);
        }

        /// <summary>
        /// 修改页面提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditPost(BcdFileListViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                string name = vm.TheBcdProjectInfo.P_Name;
                string code = vm.TheBcdProjectInfo.P_Code;
                string unit = vm.TheBcdProjectInfo.P_Unit;
                vm.TheBcdProjectInfo = BcdProjectUIRule.GetEntityById(new Guid(vm.TheBcdProjectInfo.P_ID));
                vm.TheBcdProjectInfo.P_Name = name;
                vm.TheBcdProjectInfo.P_Code = code;
                vm.TheBcdProjectInfo.P_Unit = unit;
                vm.TheBcdProjectInfo.P_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdProjectInfo.P_OperateUserName = GetLoginUserRealName;
                vm.TheBcdProjectInfo.P_OperateTime = DateTime.Now;
                BcdProjectUIRule.UpdateEntity(vm.TheBcdProjectInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功",
                    type = "edit",
                    proId = vm.TheBcdProjectInfo.P_ID,
                    pId = "null",
                    id = vm.TheBcdProjectInfo.P_ID + vm.TheBcdProjectInfo.Fd_ID,
                    name = vm.TheBcdProjectInfo.P_Section + vm.TheBcdProjectInfo.Fd_Name,
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(string proId, string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (id != null && id != "null")
                    id = id.Replace(proId, "");
                BcdProjectUIRule.DeleteEntity(proId, id);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    type = "delete",
                    proId = proId,
                    pId = "",
                    id = proId + id,
                    name = "",
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        #endregion

        #region 右侧 信息查看
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public ActionResult Detail(string tab_i, string type, string proId, string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            ViewBag.Tab_i = tab_i;
            ViewBag.Type = type;
            ViewBag.ProId = proId;
            ViewBag.FId = id;
            if (id != null && id != "null" && id != "")
            {
                id = id.Replace(proId, "");
                //项目与案卷类型详情
                BcdProjectInfo info = new BcdProjectInfo();
                if (proId != null && proId != "null")
                {
                    BcdFiledataInfo fdInfo = BcdFiledataUIRule.GetEntityById(new Guid(id));
                    if (fdInfo.Fd_PID == null || fdInfo.Fd_PID == "null")
                        info = BcdProjectUIRule.GetEntityById(new Guid(proId));
                    else
                    {
                        List<BcdProjectfileInfo> fList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_ID='" + id + "' ");
                        BcdProjectfileInfo fInfo = new BcdProjectfileInfo();
                        if (fList.Count > 0)
                        {
                            fInfo = fList[0];
                            info.P_Name = fInfo.P_Name;
                            info.P_Code = fInfo.P_Code;
                            info.Fd_ID = fInfo.Fd_ID;
                            info.Fd_Name = fInfo.Fd_Name;
                            info.Fd_UType = fInfo.Fd_UType;
                            info.P_UType = fInfo.Fd_UType;
                            info.P_Section = fInfo.P_Section;
                            info.P_Unit = fInfo.P_Unit;
                            info.Fd_Code = fInfo.Fd_Code;
                            info.Fd_Kept = fInfo.Fd_Kept;
                            info.Fd_Kit = fInfo.Fd_Kit;
                            info.Fd_Table = fInfo.Fd_Table;
                            info.Fd_Remark = fInfo.Fd_Remark;
                        }
                    }
                    if (info.Fd_Code != "")
                    {
                        if (info.Fd_UType == "项目法人" || info.Fd_UType == "相关单位")
                            info.Fd_Code = info.P_Code + info.Fd_Code.Substring(info.Fd_Code.IndexOf('/'));
                        if (info.Fd_UType == "设计单位")
                            info.Fd_Code = info.P_Code + "（设计）" + info.Fd_Code.Substring(info.Fd_Code.IndexOf('/'));
                        if (info.Fd_UType == "施工单位" || info.Fd_UType == "监理单位" || info.Fd_UType == "施工、监理")
                            info.Fd_Code = info.P_Code + "（" + info.P_Section + "）" + info.Fd_Code.Substring(info.Fd_Code.IndexOf('/'));
                    }
                }
                vm.TheBcdProjectInfo = info;
            }
            else
            {
                vm.TheBcdProjectInfo = new BcdProjectInfo();
            }
            return View(vm);
        }
        #endregion

        #region 案卷管理
        #region 案卷列表
        /// <summary>
        /// 档案列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="proId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult FiledataList(string type, string proId, string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            ViewBag.Type = type;
            //第几页
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            if (id != null && id != "null" && id != "")
            {
                id = id.Replace(proId, "");
                ViewBag.ProId = proId;
                ViewBag.FId = id;
                //案卷表
                string sql = " P_ID='" + proId + "' and (Fd_ID='" + id + "') ";// or Fd_PIds like '%" + id + "%'
                int totalRecord = 0;
                //当前页码
                vm.TheBcdFilelistList = BcdFilelistUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Fl_Numb", out totalRecord, sql, "asc", "V_FileDataList");
                //总条数
                ViewBag.totalcount = totalRecord;
                //总页数
                ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            }
            else
            {
                vm.TheBcdFilelistList = new List<BcdFilelistInfo>();
            }
            return PartialView(vm);
        }
        /// <summary>
        /// 案卷列表分页
        /// </summary>
        /// <returns></returns>
        public ActionResult FiledataListSplitPage()
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.Type = Request.Form["Type"];
            ViewBag.ProId = Request.Form["ProId"];
            ViewBag.FId = Request.Form["FId"];
            if (ViewBag.pageNum == 0 || ViewBag.numPerPage == 0)
                return FiledataList(ViewBag.Type, ViewBag.ProId, ViewBag.FId);
            else
            {
                //工程项目与归档类型与案卷目录对应关系表
                string sql = " P_ID='" + ViewBag.ProId + "' and (Fd_ID='" + ViewBag.FId + "') ";// or Fd_PIds like '%" + ViewBag.FId + "%'
                ViewBag.iput_code = Request.Form["iput_code"];
                ViewBag.iput_numb1 = Request.Form["iput_numb1"];
                ViewBag.iput_numb2 = Request.Form["iput_numb2"];
                ViewBag.iput_name = Request.Form["iput_name"];
                if (ViewBag.iput_code != "")
                    sql += " and Fl_Code like '%" + ViewBag.iput_code + "%' ";
                if (ViewBag.iput_numb1 != "")
                {
                    if (IsNumeric(ViewBag.iput_numb1))
                        sql += " and Fl_Numb>=" + ViewBag.iput_numb1;
                }
                if (ViewBag.iput_numb2 != "")
                {
                    if (IsNumeric(ViewBag.iput_numb2))
                        sql += " and Fl_Numb<=" + ViewBag.iput_numb2;
                }
                if (ViewBag.iput_name != "")
                    sql += " and Fl_Item+Fl_FileTitle+Fl_FileName like '%" + ViewBag.iput_name + "%' ";
                //案卷目录表
                vm.TheBcdFilelistList = BcdFilelistUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Fl_Numb", out totalRecord, sql, "asc", "V_FileDataList");
                //总条数
                ViewBag.totalcount = totalRecord;
                //总页数
                ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            }
            return View("FiledataList", vm);
        }

        /// <summary>  
        /// 判断是否是数字  
        /// </summary>  
        /// <param name="str">字符串</param>  
        /// <returns>bool</returns>  
        public bool IsNumeric(string str)
        {
            if (str == null || str.Length == 0)
                return false;
            System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
            byte[] bytestr = ascii.GetBytes(str);
            foreach (byte c in bytestr)
            {
                if (c < 48 || c > 57)
                {
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region 案卷增删改查
        /// <summary>
        /// 添加案卷
        /// </summary>
        /// <returns></returns>
        public ActionResult FileAdd(string proId, string fId, bool insert)
        {
            ViewBag.insert = insert;
            BcdFileListViewModel vm = new BcdFileListViewModel();
            //案卷目录表
            BcdFilelistInfo info = new BcdFilelistInfo();
            BcdProjectInfo pInfo = BcdProjectUIRule.GetEntityById(new Guid(proId));
            info.Fl_ID = Guid.NewGuid().ToString().ToUpper();
            info.Fl_FileType = pInfo.Fd_Name;
            info.Fl_Code = pInfo.Fd_Code;
            List<BcdProjectfileInfo> fList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_ID='" + fId + "' ");
            if (fList.Count > 0)
            {
                BcdProjectfileInfo fInfo = new BcdProjectfileInfo();
                fInfo = fList[0];
                if (fInfo.Fd_Code != "")
                {
                    if (fInfo.Fd_UType == "项目法人" || fInfo.Fd_UType == "相关单位")
                        info.Fl_Code = fInfo.P_Code + fInfo.Fd_Code.Substring(fInfo.Fd_Code.IndexOf('/'));
                    if (fInfo.Fd_UType == "设计单位")
                        info.Fl_Code = fInfo.P_Code + "（设计）" + fInfo.Fd_Code.Substring(fInfo.Fd_Code.IndexOf('/'));
                    if (fInfo.Fd_UType == "施工单位" || fInfo.Fd_UType == "监理单位" || fInfo.Fd_UType == "施工、监理")
                        info.Fl_Code = fInfo.P_Code + "（" + fInfo.P_Section + "）" + fInfo.Fd_Code.Substring(fInfo.Fd_Code.IndexOf('/'));
                }
            }
            else
            {
                if (pInfo.Fd_Code != "")
                {
                    if (pInfo.Fd_UType == "项目法人" || pInfo.Fd_UType == "相关单位")
                        info.Fl_Code = pInfo.P_Code + pInfo.Fd_Code.Substring(pInfo.Fd_Code.IndexOf('/'));
                    if (pInfo.Fd_UType == "设计单位")
                        info.Fl_Code = pInfo.P_Code + "（设计）" + pInfo.Fd_Code.Substring(pInfo.Fd_Code.IndexOf('/'));
                    if (pInfo.Fd_UType == "施工单位" || pInfo.Fd_UType == "监理单位" || pInfo.Fd_UType == "施工、监理")
                        info.Fl_Code = pInfo.P_Code + "（" + pInfo.P_Section + "）" + pInfo.Fd_Code.Substring(pInfo.Fd_Code.IndexOf('/'));
                }
            }
            if (!insert)
            {
                //案卷流水号
                string sql = " Fl_Code='" + info.Fl_Code + "' ";
                if (info.Fl_Code.IndexOf(".") >= 0)
                {
                    string code = info.Fl_Code;
                    string title = code.Substring(0, code.IndexOf('/'));
                    string numb = code.Substring(code.IndexOf('/'), 4);
                    string number = numb.Substring(1, 1);
                    if (number == "3")
                        numb = numb.Substring(0, 2);
                    sql = " Fl_Code like '" + title + numb + "%' ";
                }
                int count = BcdFilelistUIRule.GetNumbBySql(sql);
                if (count == 0)
                {
                    count = BcdFilelistUIRule.GetCountBySql(sql);
                }
                //流水号 自增长
                info.Fl_Numb = count + 1;
            }
            info.Fl_Item = pInfo.P_Name;
            info.Fl_Unit = pInfo.P_Unit;
            info.Fl_Kept = pInfo.Fd_Kept;
            vm.TheBcdFilelistInfo = info;

            //工程项目与归档类型与案卷目录对应关系表
            BcdFiledatalistInfo fdlInfo = new BcdFiledatalistInfo();
            fdlInfo.P_ID = proId;
            fdlInfo.Fd_ID = fId;
            fdlInfo.Fl_ID = info.Fl_ID;
            vm.TheBcdFiledatalistInfo = fdlInfo;
            return View(vm);
        }
        /// <summary>
        /// 添加案卷页面提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FileAddPost(BcdFileListViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                bool insert = Convert.ToBoolean(Request.Form["iput_insert"]);
                if (insert)
                {
                    //案卷流水号
                    string sql = " Fl_Code='" + vm.TheBcdFilelistInfo.Fl_Code + "' ";
                    if (vm.TheBcdFilelistInfo.Fl_Code.IndexOf(".") >= 0)
                    {
                        string code = vm.TheBcdFilelistInfo.Fl_Code;
                        string title = code.Substring(0, code.IndexOf('/'));
                        string numb = code.Substring(code.IndexOf('/'), 4);
                        string number = numb.Substring(1, 1);
                        if (number == "3")
                            numb = numb.Substring(0, 2);
                        sql = " Fl_Code like '" + title + numb + "%' ";
                    }
                    int count = BcdFilelistUIRule.GetCountBySql(sql + " and Fl_Numb=" + vm.TheBcdFilelistInfo.Fl_Numb);
                    if (count > 0)
                    {
                        //档案流水号已存在 将此流水号及比它大的流水号+1
                        BcdFilelistUIRule.InsertNumb(sql + " and Fl_Numb>=" + vm.TheBcdFilelistInfo.Fl_Numb);
                    }
                }
                //添加  案卷目录表
                vm.TheBcdFilelistInfo.Fl_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdFilelistInfo.Fl_OperateUserName = GetLoginUserRealName;
                vm.TheBcdFilelistInfo.Fl_OperateTime = DateTime.Now;
                BcdFilelistUIRule.InsertEntity(vm.TheBcdFilelistInfo);
                //添加  工程项目与归档类型与案卷目录对应关系表
                BcdFiledatalistUIRule.InsertEntity(vm.TheBcdFiledatalistInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "add",
                    navTabId = "",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 修改案卷
        /// </summary>
        /// <returns></returns>
        public ActionResult FileEdit(string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            vm.TheBcdFilelistInfo = BcdFilelistUIRule.GetEntityById(new Guid(id));
            return View(vm);
        }
        /// <summary>
        /// 修改案卷页面提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FileEditPost(BcdFileListViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //修改  案卷目录表
                vm.TheBcdFilelistInfo.Fl_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdFilelistInfo.Fl_OperateUserName = GetLoginUserRealName;
                vm.TheBcdFilelistInfo.Fl_OperateTime = DateTime.Now;
                BcdFilelistUIRule.UpdateEntity(vm.TheBcdFilelistInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功",
                    type = "eidt",
                    navTabId = "",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 删除案卷
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FileDel(string proId, string fId, string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //删除关联表
                BcdFiledatalistInfo fdlInfo = new BcdFiledatalistInfo();
                fdlInfo.P_ID = proId;
                fdlInfo.Fd_ID = fId;
                fdlInfo.Fl_ID = id;
                BcdFiledatalistUIRule.DeleteEntity(fdlInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    type = "del",
                    navTabId = "",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 查看案卷信息
        /// </summary>
        /// <returns></returns>
        public ActionResult FileDetail(string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            vm.TheBcdFilelistInfo = BcdFilelistUIRule.GetEntityById(new Guid(id));
            vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListBySql(" Fl_ID='" + id + "' ");

            vm.TheBcdVolumedirectoryList.ForEach(t =>
            {
                t.FilePath = FilePath(t.Vd_ID, t.P_ID, t.Fd_ID);
            });
            return View(vm);
        }
        #endregion

        #region 卷内目录管理
        /// <summary>
        /// 卷内目录首页
        /// </summary>
        /// <returns></returns>
        public ActionResult FileVolume(string type, string proId, string fId, string id)
        {
            ViewBag.Type = type;
            ViewBag.IProId = proId;
            ViewBag.IFId = fId;

            BcdFileListViewModel vm = new BcdFileListViewModel();
            vm.TheBcdFilelistInfo = BcdFilelistUIRule.GetEntityById(new Guid(id));
            List<BcdFiledatalistInfo> fdlList = BcdFiledatalistUIRule.GetEntityListBySql(" Fl_ID='" + id + "' ");
            if (fdlList.Count > 0)
            {
                BcdFiledatalistInfo fdlInfo = fdlList[0];
                ViewBag.ProId = fdlInfo.P_ID;
                ViewBag.FId = fdlInfo.Fd_ID;
            }
            return View(vm);
        }
        /// <summary>
        /// 卷内目录列表
        /// </summary>
        /// <returns></returns>
        public ActionResult VolumedirectoryList(string id)
        {

            BcdFileListViewModel vm = new BcdFileListViewModel();
            vm.TheBcdFilelistInfo = BcdFilelistUIRule.GetEntityById(new Guid(id));
            vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListBySql(" Fl_ID='" + id + "' ");
            return PartialView(vm);
        }
        /// <summary>
        /// 归档材料列表
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="fId"></param>
        /// <returns></returns>
        public ActionResult FileVolumeList(string flId, string proId, string fId)
        {
            ViewBag.FlId = flId;
            BcdFileListViewModel vm = new BcdFileListViewModel();
            string sql = " Fl_ID is null and Vd_State=1 and P_ID='" + proId + "' and (Fd_ID='" + fId + "') ";// or Fd_PIds like '%" + fId + "%'
            vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListBySql(sql);
            return View(vm);
        }
        /// <summary>
        /// 添加未归档文件提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FileVolumeAdd(BcdFileListViewModel vm)
        {
            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            string flid = Request.Form["input_flid"];
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                string ids = Request.Form["ids"];
                foreach (string id in ids.Split(','))
                {
                    BcdVolumedirectoryInfo vInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
                    vInfo.Vd_State = 2;
                    vInfo.Fl_ID = flid;
                    vInfo.Vd_OperateTime = DateTime.Now;
                    vInfo.Vd_OperateUserID = GetLoginUserId;
                    vInfo.Vd_OperateUserName = GetLoginUserName;
                    BcdVolumedirectoryUIRule.UpdateEntity(vInfo);
                    List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + vInfo.P_ID + "' and Fd_ID='" + vInfo.Fd_ID + "' ");
                    if (pfList.Count > 0)
                        pInfo = pfList[0];
                }
            });
            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    flid=flid,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 拆出文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult VolumeSplit(string id)
        {
            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                BcdVolumedirectoryUIRule.SplitVolume(id);
                BcdVolumedirectoryInfo info = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
                List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + info.P_ID + "' and Fd_ID='" + info.Fd_ID + "' ");
                if (pfList.Count > 0)
                    pInfo = pfList[0];
            });

            dynamic jsonReturn = new ExpandoObject();
            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "拆出成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "拆出失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 卷内目录序号、页数更新
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult VolumeListUpdate(BcdFileListViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                foreach (BcdVolumedirectoryInfo info in vm.TheBcdVolumedirectoryList)
                {
                    BcdVolumedirectoryInfo vInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(info.Vd_ID));
                    vInfo.Vd_Order = info.Vd_Order;
                    vInfo.Vd_Page = info.Vd_Page;
                    vInfo.Vd_OperateTime = DateTime.Now;
                    vInfo.Vd_OperateUserID = GetLoginUserId;
                    vInfo.Vd_OperateUserName = GetLoginUserName;
                    BcdVolumedirectoryUIRule.UpdateEntity(vInfo);
                }
            });
            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "保存成功",
                    type = "add",
                    navTabId = "",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "保存失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 关联案卷
        /// <summary>
        /// 关联案卷
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="fId">材料类型id</param>
        /// <returns></returns>
        public ActionResult FileRelation(string proId, string fId)
        {
            ViewBag.proId = proId;
            ViewBag.fId = fId;
            return View();
        }
        /// <summary>
        /// 关联案卷Json
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="fId">材料类型id</param>
        /// <returns></returns>
        public JsonResult FileRelationJson(string proId, string fId)
        {
            BcdFiledataInfo fdInfo = BcdFiledataUIRule.GetEntityById(new Guid(fId));
            List<BcdFiledataInfo> fdList = BcdFiledataUIRule.GetEntityListBySql(" Fd_PID='" + fdInfo.Fd_PID + "'");
            string fdIds = "";
            if (fdInfo.Fd_PID == null)
                fdIds = "'" + fId + "'";
            else
            {
                fdIds = "'" + fdInfo.Fd_PID + "',";
                foreach (BcdFiledataInfo info in fdList)
                {
                    fdIds += "'" + info.Fd_ID + "',";
                }
            }
            //工程项目与归档类型与案卷目录对应关系表
            List<BcdFiledatalistInfo> fdlList = BcdFiledatalistUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_ID in (" + fdIds.Trim(',') + ") ");
            string flIds = "";
            foreach (BcdFiledatalistInfo info in fdlList)
            {
                flIds += "'" + info.Fl_ID + "',";
            }
            flIds = flIds.Trim(',');
            string sql = " 1!=1 ";
            if (flIds != "")
                sql = " Fl_ID in (" + flIds + ") ";
            //案卷目录表
            List<BcdFilelistInfo> flList = BcdFilelistUIRule.GetEntityListBySql(sql);
            List<treeNodes> treenodes = new List<treeNodes>();
            foreach (BcdFilelistInfo info in flList)
            {
                //读取 工程项目与归档类型与案卷目录对应关系表
                List<BcdFiledatalistInfo> fdlList1 = BcdFiledatalistUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_ID ='" + fId + "' and Fl_ID='" + info.Fl_ID + "' ");
                //如果尚未关联
                if (fdlList1.Count == 0)
                {
                    //显示在弹出框中以备勾选
                    treeNodes node = new treeNodes();
                    node.id = info.Fl_ID;
                    node.name = info.Fl_Code + "-" + info.Fl_Numb.ToString("D3") + "(" + info.Fl_FileName + ")";
                    treenodes.Add(node);
                }

            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 关联案卷Post提交
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="fId">材料类型id</param>
        /// <param name="flIds">案卷id集合</param>
        /// <returns></returns>
        public JsonResult FileRelationPost(string proId, string fId, string flIds)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                flIds = flIds.Trim(',');
                foreach (string flId in flIds.Split(','))
                {
                    List<BcdFiledatalistInfo> fdlList = BcdFiledatalistUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_ID='" + fId + "' and Fl_ID='" + flId + "' ");
                    if (fdlList.Count == 0)
                    {
                        //如果未关联 则添加
                        BcdFiledatalistInfo info = new BcdFiledatalistInfo();
                        info.P_ID = proId;
                        info.Fd_ID = fId;
                        info.Fl_ID = flId;
                        BcdFiledatalistUIRule.InsertEntity(info);
                    }
                }
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "关联成功",
                    type = "relation",
                    navTabId = "",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "关联失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 导出
        /// <summary>
        /// 导出案卷
        /// </summary>
        /// <returns></returns>
        public FileResult ExportFile(string id)
        {
            string fileName = "案卷.docx";
            BcdFilelistInfo info = BcdFilelistUIRule.GetEntityById(new Guid(id));
            info.Fl_Item = info.Fl_Item + info.Fl_FileTitle + info.Fl_FileName;
            List<BcdVolumedirectoryInfo> list = BcdVolumedirectoryUIRule.GetEntityListBySql(" Fl_ID='" + id + "' ");
            var ret = base.ExportFileToWord(fileName, info, list);
            return ret;
        }

        /// <summary>
        /// 导出案卷目录
        /// </summary>
        /// <returns></returns>
        public FileResult ExportList(string proId, string fId, string code, string numb1, string numb2, string name)
        {
            //工程项目与归档类型与案卷目录对应关系表
            string sql = " P_ID='" + proId + "' and (Fd_ID='" + fId + "') ";// or Fd_PIds like '%" + fId + "%'
            if (code != "")
                sql += " and Fl_Code like '%" + code + "%' ";
            if (numb1 != "")
            {
                if (IsNumeric(numb1))
                    sql += " and Fl_Numb>=" + numb1;
            }
            if (numb2 != "")
            {
                if (IsNumeric(numb2))
                    sql += " and Fl_Numb<=" + numb2;
            }
            if (name != "")
                sql += " and Fl_Item+Fl_FileTitle+Fl_FileName like '%" + name + "%' ";
            List<BcdFilelistInfo> list = BcdFilelistUIRule.GetEntityListBySql(sql);
            string fileName = "案卷目录.docx";
            int i = 0;
            foreach (BcdFilelistInfo info in list)
            {
                i++;
                info.index = i.ToString();
                info.Fl_Code = info.Fl_Code + info.Fl_Numb.ToString("D3");
                info.Fl_FileName = info.Fl_Item + info.Fl_FileTitle + info.Fl_FileName;
            }
            return base.ExportToWord(fileName, list);
        }
        #endregion
        #endregion

        #region 组卷
        /// <summary>
        /// 组卷
        /// </summary>
        /// <returns></returns>
        public ActionResult FileCreat(string type, string proId, string fId)
        {
            ViewBag.Type = type;
            ViewBag.ProId = proId;
            ViewBag.FId = fId;

            BcdFileListViewModel vm = new BcdFileListViewModel();
            //案卷目录表
            BcdFilelistInfo info = new BcdFilelistInfo();
            BcdProjectInfo pInfo = BcdProjectUIRule.GetEntityById(new Guid(proId));
            info.Fl_ID = Guid.NewGuid().ToString().ToUpper();
            info.Fl_FileType = pInfo.Fd_Name;
            info.Fl_Code = pInfo.Fd_Code;
            List<BcdProjectfileInfo> fList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + proId + "' and Fd_ID='" + fId + "' ");
            if (fList.Count > 0)
            {
                BcdProjectfileInfo fInfo = new BcdProjectfileInfo();
                fInfo = fList[0];
                if (fInfo.Fd_Code != "")
                {
                    if (fInfo.Fd_UType == "项目法人" || fInfo.Fd_UType == "相关单位")
                        info.Fl_Code = fInfo.P_Code + fInfo.Fd_Code.Substring(fInfo.Fd_Code.IndexOf('/'));
                    if (fInfo.Fd_UType == "设计单位")
                        info.Fl_Code = fInfo.P_Code + "（设计）" + fInfo.Fd_Code.Substring(fInfo.Fd_Code.IndexOf('/'));
                    if (fInfo.Fd_UType == "施工单位" || fInfo.Fd_UType == "监理单位" || fInfo.Fd_UType == "施工、监理")
                        info.Fl_Code = fInfo.P_Code + "（" + fInfo.P_Section + "）" + fInfo.Fd_Code.Substring(fInfo.Fd_Code.IndexOf('/'));
                }
            }
            else
            {
                if (pInfo.Fd_Code != "")
                {
                    if (pInfo.Fd_UType == "项目法人" || pInfo.Fd_UType == "相关单位")
                        info.Fl_Code = pInfo.P_Code + pInfo.Fd_Code.Substring(pInfo.Fd_Code.IndexOf('/'));
                    if (pInfo.Fd_UType == "设计单位")
                        info.Fl_Code = pInfo.P_Code + "（设计）" + pInfo.Fd_Code.Substring(pInfo.Fd_Code.IndexOf('/'));
                    if (pInfo.Fd_UType == "施工单位" || pInfo.Fd_UType == "监理单位" || pInfo.Fd_UType == "施工、监理")
                        info.Fl_Code = pInfo.P_Code + "（" + pInfo.P_Section + "）" + pInfo.Fd_Code.Substring(pInfo.Fd_Code.IndexOf('/'));
                }
            }
            string sql = " Fl_Code='" + info.Fl_Code + "' ";
            if (info.Fl_Code.IndexOf(".") >= 0)
            {
                string code = info.Fl_Code;
                string title = code.Substring(0, code.IndexOf('/'));
                string numb = code.Substring(code.IndexOf('/'), 4);
                string number = numb.Substring(1, 1);
                if (number == "3")
                    numb = numb.Substring(0, 2);
                sql = " Fl_Code like '" + title + numb + "%' ";
            }
            int count = BcdFilelistUIRule.GetNumbBySql(sql);
            if (count == 0)
            {
                List<BcdFilelistInfo> flList = BcdFilelistUIRule.GetEntityListBySql(sql);
                count = flList.Count;
            }
            //自增长
            info.Fl_Numb = count + 1;
            info.Fl_Item = pInfo.P_Name;
            info.Fl_Unit = pInfo.P_Unit;
            info.Fl_Kept = pInfo.Fd_Kept;
            vm.TheBcdFilelistInfo = info;

            //工程项目与归档类型与案卷目录对应关系表
            BcdFiledatalistInfo fdlInfo = new BcdFiledatalistInfo();
            fdlInfo.P_ID = proId;
            fdlInfo.Fd_ID = fId;
            fdlInfo.Fl_ID = info.Fl_ID;
            vm.TheBcdFiledatalistInfo = fdlInfo;
            return View(vm);
        }
        /// <summary>
        /// 未归档文件选择列表
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult FileCheckList(string fileIds, string proId, string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            ViewBag.fileIds = fileIds;
            //第几页
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 10;
            if (id != null && id != "null" && id != "")
            {
                id = id.Replace(proId, "");
                ViewBag.ProId = proId;
                ViewBag.FId = id;
                //案卷目录表 or Fl_ID=''
                string sql = " Fl_ID is null and Vd_State=1 and P_ID='" + proId + "' and Fd_ID='" + id + "'";
                int totalRecord = 0;
                //当前页码
                vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Vd_Order", out totalRecord, sql, "asc");
                //vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListBySql(sql);
                //总条数
                ViewBag.totalcount = totalRecord;
                //总页数
                ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            }
            else
            {
                vm.TheBcdFilelistList = new List<BcdFilelistInfo>();
            }
            return PartialView(vm);
        }
        /// <summary>
        /// 未归档文件选择列表分页
        /// </summary>
        /// <returns></returns>
        public ActionResult FileCheckListSplitPage()
        {
            ViewBag.fileIds = Request.Form["fileIds"];
            BcdFileListViewModel vm = new BcdFileListViewModel();
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.ProId = Request.Form["ProId"];
            ViewBag.FId = Request.Form["FId"];
            if (ViewBag.pageNum == 0 || ViewBag.numPerPage == 0)
                return FileCheckList(ViewBag.Type, ViewBag.ProId, ViewBag.FId);
            else
            {
                //工程项目与归档类型与案卷目录对应关系表
                string sql = " Fl_ID is null and Vd_State=1 and P_ID='" + ViewBag.ProId + "' and Fd_ID='" + ViewBag.FId + "' ";
                ViewBag.iput_code = Request.Form["iput_code"];
                ViewBag.iput_name = Request.Form["iput_name"];
                if (ViewBag.iput_code != "")
                    sql += " and Vd_Code like '%" + ViewBag.iput_code + "%' ";
                if (ViewBag.iput_name != "")
                    sql += " and Vd_Name like '%" + ViewBag.iput_name + "%' ";
                //案卷目录表
                vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Vd_Order", out totalRecord, sql, "asc");
                //总条数
                ViewBag.totalcount = totalRecord;
                //总页数
                ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            }
            return View("FileCheckList", vm);
        }

        /// <summary>
        /// 组卷提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FileCreatPost(BcdFileListViewModel vm)
        {
            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //添加  案卷目录表
                vm.TheBcdFilelistInfo.Fl_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdFilelistInfo.Fl_OperateUserName = GetLoginUserRealName;
                vm.TheBcdFilelistInfo.Fl_OperateTime = DateTime.Now;
                BcdFilelistUIRule.InsertEntity(vm.TheBcdFilelistInfo);
                //添加  工程项目与归档类型与案卷目录对应关系表
                BcdFiledatalistUIRule.InsertEntity(vm.TheBcdFiledatalistInfo);
                foreach (BcdVolumedirectoryInfo info in vm.TheBcdVolumedirectoryList)
                {
                    BcdVolumedirectoryInfo vInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(info.Vd_ID));
                    vInfo.Vd_State = 2;
                    vInfo.Fl_ID = vm.TheBcdFilelistInfo.Fl_ID;
                    vInfo.Vd_Order = info.Vd_Order;
                    vInfo.Vd_Code = info.Vd_Code;
                    vInfo.Vd_Name = info.Vd_Name;
                    vInfo.Vd_Page = info.Vd_Page;

                    vInfo.Vd_OperateTime = DateTime.Now;
                    vInfo.Vd_OperateUserID = GetLoginUserId;
                    vInfo.Vd_OperateUserName = GetLoginUserName;
                    BcdVolumedirectoryUIRule.UpdateEntity(vInfo);
                }

                List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + vm.TheBcdFiledatalistInfo.P_ID + "' and Fd_ID='" + vm.TheBcdFiledatalistInfo.Fd_ID + "' ");
                if (pfList.Count > 0)
                    pInfo = pfList[0];
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "组卷成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "组卷失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 拆卷
        /// <summary>
        /// 拆卷弹出框
        /// </summary>
        /// <param name="type"></param>
        /// <param name="proId"></param>
        /// <param name="fId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult FileSplit(string type, string proId, string fId, string id)
        {
            ViewBag.Type = type;
            ViewBag.ProId = proId;
            ViewBag.FId = fId;
            BcdFileListViewModel vm = new BcdFileListViewModel();
            //卷内目录列表
            vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListBySql(" Fl_ID='" + id + "' ");
            //旧案卷信息
            BcdFilelistInfo info = BcdFilelistUIRule.GetEntityById(new Guid(id));
            //新案卷信息
            vm.TheBcdFilelistInfo = new BcdFilelistInfo();
            vm.TheBcdFilelistInfo.Fl_ID = Guid.NewGuid().ToString().ToUpper();
            vm.TheBcdFilelistInfo.Fl_FileType = info.Fl_FileType;
            vm.TheBcdFilelistInfo.Fl_Code = info.Fl_Code;
            string sql = " Fl_Code='" + info.Fl_Code + "' ";
            if (info.Fl_Code.IndexOf(".") >= 0)
            {
                string code = info.Fl_Code;
                string title = code.Substring(0, code.IndexOf('/'));
                string numb = code.Substring(code.IndexOf('/'), 4);
                string number = numb.Substring(1, 1);
                if (number == "3")
                    numb = numb.Substring(0, 2);
                sql = " Fl_Code like '" + title + numb + "%' ";
            }
            int count = BcdFilelistUIRule.GetNumbBySql(sql);
            if (count == 0)
            {
                List<BcdFilelistInfo> flList = BcdFilelistUIRule.GetEntityListBySql(sql);
                count = flList.Count;
            }
            //自增长
            vm.TheBcdFilelistInfo.Fl_Numb = count + 1;
            vm.TheBcdFilelistInfo.Fl_Item = info.Fl_Item;
            vm.TheBcdFilelistInfo.Fl_Unit = info.Fl_Unit;
            vm.TheBcdFilelistInfo.Fl_Kept = info.Fl_Kept;
            //工程项目与归档类型与案卷目录对应关系表
            vm.TheBcdFiledatalistInfo = new BcdFiledatalistInfo();
            vm.TheBcdFiledatalistInfo = BcdFiledatalistUIRule.GetEntityListBySql(" Fl_ID='" + info.Fl_ID + "' ")[0];
            vm.TheBcdFiledatalistInfo.Fl_ID = vm.TheBcdFilelistInfo.Fl_ID;
            return View(vm);
        }
        /// <summary>
        /// 拆卷提交
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FileSplitPost(BcdFileListViewModel vm)
        {
            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (vm.TheBcdFilelistInfo != null)
                {
                    //拆出新卷
                    //添加  案卷目录表
                    vm.TheBcdFilelistInfo.Fl_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                    vm.TheBcdFilelistInfo.Fl_OperateUserName = GetLoginUserRealName;
                    vm.TheBcdFilelistInfo.Fl_OperateTime = DateTime.Now;
                    BcdFilelistUIRule.InsertEntity(vm.TheBcdFilelistInfo);
                    //添加  工程项目与归档类型与案卷目录对应关系表
                    BcdFiledatalistUIRule.InsertEntity(vm.TheBcdFiledatalistInfo);
                    //修改 卷内目录
                    string ids = Request.Form["ids"];
                    foreach (string id in ids.Split(','))
                    {
                        BcdVolumedirectoryInfo vInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
                        vInfo.Fl_ID = vm.TheBcdFilelistInfo.Fl_ID;
                        vInfo.Vd_OperateTime = DateTime.Now;
                        vInfo.Vd_OperateUserID = GetLoginUserId;
                        vInfo.Vd_OperateUserName = GetLoginUserName;
                        BcdVolumedirectoryUIRule.UpdateEntity(vInfo);
                        List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + vInfo.P_ID + "' and Fd_ID='" + vInfo.Fd_ID + "' ");
                        if (pfList.Count > 0)
                            pInfo = pfList[0];
                    }
                }
                else
                {
                    //直接拆卷
                    string ids = Request.Form["ids"];
                    foreach (string id in ids.Split(','))
                    {
                        BcdVolumedirectoryUIRule.SplitVolume(id);
                        BcdVolumedirectoryInfo vInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
                        List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + vInfo.P_ID + "' and Fd_ID='" + vInfo.Fd_ID + "' ");
                        if (pfList.Count > 0)
                            pInfo = pfList[0];
                    }
                }
            });
            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "拆卷成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "拆卷失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 未归档文件管理
        #region 未归档文件列表
        /// <summary>
        /// 未归档文件列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="proId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult VolumeList(string type, string proId, string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            ViewBag.Type = type;
            //第几页
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            if (id != null && id != "null" && id != "")
            {
                id = id.Replace(proId, "");
                ViewBag.ProId = proId;
                ViewBag.FId = id;
                //案卷目录表 or Fl_ID=''
                string sql = " P_ID='" + proId + "' and Fd_ID='" + id + "' ";// and Fl_ID is null
                int totalRecord = 0;
                //当前页码
                vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Vd_State,Vd_Order", out totalRecord, sql, "asc", "V_VolumeDirectory");
                vm.TheBcdVolumedirectoryList.ForEach(t =>
                {
                    t.FilePath = FilePath(t.Vd_ID, t.P_ID, t.Fd_ID);
                    t.FileCount = GetFilesCount(Server.MapPath(String.Format("~/Files/{0}", t.FilePath)), 0);
                });
                //总条数
                ViewBag.totalcount = totalRecord;
                //总页数
                ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            }
            else
            {
                vm.TheBcdFilelistList = new List<BcdFilelistInfo>();
            }
            return PartialView(vm);
        }
        //public int GetFilesCount(string path, int count)
        //{
        //    if (!System.IO.Directory.Exists(path))
        //    {
        //        return 0;
        //    }
        //    //如果嵌套文件夹很多，可以开子线程去统计
        //    count += System.IO.Directory.GetFiles(path).Length;
        //    foreach (var folder in System.IO.Directory.GetDirectories(path))
        //    {
        //        GetFilesCount(folder, count);
        //    }
        //    return count;
        //}
        /// <summary>
        /// 未归档文件列表分页
        /// </summary>
        /// <returns></returns>
        public ActionResult VolumeListSplitPage()
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.Type = Request.Form["Type"];
            ViewBag.ProId = Request.Form["ProId"];
            ViewBag.FId = Request.Form["FId"];
            if (ViewBag.pageNum == 0 || ViewBag.numPerPage == 0)
                return VolumeList(ViewBag.Type, ViewBag.ProId, ViewBag.FId);
            else
            {
                //工程项目与归档类型与案卷目录对应关系表
                string sql = " P_ID='" + ViewBag.ProId + "' and Fd_ID='" + ViewBag.FId + "' ";// and Fl_ID is null
                ViewBag.iput_code = Request.Form["iput_code"];
                ViewBag.iput_name = Request.Form["iput_name"];
                if (ViewBag.iput_code != "")
                    sql += " and Vd_Code like '%" + ViewBag.iput_code + "%' ";
                if (ViewBag.iput_name != "")
                    sql += " and Vd_Name like '%" + ViewBag.iput_name + "%' ";
                //案卷目录表
                vm.TheBcdVolumedirectoryList = BcdVolumedirectoryUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Vd_State,Vd_Order", out totalRecord, sql, "asc", "V_VolumeDirectory");
                vm.TheBcdVolumedirectoryList.ForEach(t =>
                {
                    t.FilePath = FilePath(t.Vd_ID, t.P_ID, t.Fd_ID);
                    t.FileCount = GetFilesCount(Server.MapPath(String.Format("~/Files/{0}", t.FilePath)), 0);
                });
                //总条数
                ViewBag.totalcount = totalRecord;
                //总页数
                ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            }
            return View("VolumeList", vm);
        }
        #endregion

        #region 文件夹路径
        public string FilePath(string vdid, string pid, string fdid)
        {
            string path = "";
            //项目信息
            BcdProjectInfo pInfo = BcdProjectUIRule.GetEntityById(new Guid(pid));
            BcdFiledataInfo fInfo = BcdFiledataUIRule.GetEntityById(new Guid(fdid));
            path = pInfo.P_Name + "/" + pInfo.P_Section;
            string pids = fdid;
            if (fInfo.Fd_PIds != null)
                pids = fInfo.Fd_PIds.Replace("'", "").Trim(',') + (fInfo.Fd_PIds == "" ? "" : ",") + fdid;
            foreach (string id in pids.Split(','))
            {
                BcdFiledataInfo info = BcdFiledataUIRule.GetEntityById(new Guid(id));
                path += info.Fd_Name + "/";
            }
            return path + vdid;
        }
        #endregion

        #region 未归档文件增删改查
        /// <summary>
        /// 添加未归档文件
        /// </summary>
        /// <returns></returns>
        public ActionResult VolumeAdd(string proId, string fId, string flId)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            //卷内目录信息Volumedirectory
            vm.TheBcdVolumedirectoryInfo = new BcdVolumedirectoryInfo();
            vm.TheBcdVolumedirectoryInfo.Vd_ID = Guid.NewGuid().ToString().ToUpper();
            if (proId != null && proId != "")
            {
                vm.TheBcdVolumedirectoryInfo.P_ID = proId;
                vm.TheBcdVolumedirectoryInfo.Vd_Duty = BcdProjectUIRule.GetEntityById(new Guid(proId)).P_Unit;
            }
            if (fId != null && fId != "")
                vm.TheBcdVolumedirectoryInfo.Fd_ID = fId;
            if (flId != null && flId != "")
            {
                //案卷信息
                vm.TheBcdFilelistInfo = BcdFilelistUIRule.GetEntityById(new Guid(flId));
                List<BcdVolumedirectoryInfo> vList = BcdVolumedirectoryUIRule.GetEntityListBySql(" Fl_ID='" + flId + "' ");
                vm.TheBcdVolumedirectoryInfo.Fl_ID = flId;
                vm.TheBcdVolumedirectoryInfo.Vd_Duty = vm.TheBcdFilelistInfo.Fl_Unit;
                vm.TheBcdVolumedirectoryInfo.Vd_Order = (vList.Count + 1).ToString("D2");
            }

            //附件
            var assisId = GenerateRandom();
            var assisIdRet = 0;
            CreateAssistId(BcdVolumedirectoryUIRule, assisId, out assisIdRet);
            ViewBag.AssisId = assisIdRet;

            ViewBag.FilePath = FilePath(vm.TheBcdVolumedirectoryInfo.Vd_ID, vm.TheBcdVolumedirectoryInfo.P_ID, vm.TheBcdVolumedirectoryInfo.Fd_ID);

            return View(vm);
        }
        /// <summary>
        /// 添加未归档文件提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult VolumeAddPost(BcdFileListViewModel vm)
        {
            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //添加  卷内目录表
                //操作人ID及名称
                vm.TheBcdVolumedirectoryInfo.Vd_OperateUserID = GetLoginUserId;
                vm.TheBcdVolumedirectoryInfo.Vd_OperateUserName = GetLoginUserRealName;
                vm.TheBcdVolumedirectoryInfo.Vd_OperateTime = DateTime.Now;
                if (vm.TheBcdVolumedirectoryInfo.Fl_ID != null && vm.TheBcdVolumedirectoryInfo.Fl_ID != "")
                    vm.TheBcdVolumedirectoryInfo.Vd_State = 2;
                var AssistID = Convert.ToInt32(Request.Form["AssistID"]);
                vm.TheBcdVolumedirectoryInfo.Assist_ID = AssistID;
                BcdVolumedirectoryUIRule.InsertEntity(vm.TheBcdVolumedirectoryInfo);

                List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + vm.TheBcdVolumedirectoryInfo.P_ID + "' and Fd_ID='" + vm.TheBcdVolumedirectoryInfo.Fd_ID + "' ");
                if (pfList.Count > 0)
                    pInfo = pfList[0];
            });
            //附件 继续添加时使用
            var assisId = GenerateRandom();
            var assisIdRet = 0;
            CreateAssistId(BcdVolumedirectoryUIRule, assisId, out assisIdRet);
            ViewBag.AssisId = assisIdRet;

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "",
                    vid = vm.TheBcdVolumedirectoryInfo.Fl_ID,
                    vd_id = Guid.NewGuid().ToString().ToUpper(),
                    assid = assisIdRet,
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 修改未归档文件
        /// </summary>
        /// <returns></returns>
        public ActionResult VolumeEdit(string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            //卷内目录信息Volumedirectory
            vm.TheBcdVolumedirectoryInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
            //附件
            var commonFileList = CommonFileUIRule.GetCommonFilesByConditon(vm.TheBcdVolumedirectoryInfo.Assist_ID, "FileList_Rules");//FileList_Rules  BizType类型
            ViewBag.AssisId = vm.TheBcdVolumedirectoryInfo.Assist_ID;
            ViewBag.FileIds = string.Join(",", commonFileList.Select(t => t.FILE_FILEID));
            ViewBag.DeleteIds = string.Join(",", commonFileList.Select(t => t.FILE_ID));
            ViewBag.FilePath = FilePath(vm.TheBcdVolumedirectoryInfo.Vd_ID, vm.TheBcdVolumedirectoryInfo.P_ID, vm.TheBcdVolumedirectoryInfo.Fd_ID);
            return View(vm);
        }
        /// <summary>
        /// 修改未归档文件提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult VolumeEditPost(BcdFileListViewModel vm)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                //修改  卷内目录表
                vm.TheBcdVolumedirectoryInfo.Vd_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdVolumedirectoryInfo.Vd_OperateUserName = GetLoginUserRealName;
                vm.TheBcdVolumedirectoryInfo.Vd_OperateTime = DateTime.Now;

                var AssistID = Convert.ToInt32(Request.Form["AssistID"]);
                vm.TheBcdVolumedirectoryInfo.Assist_ID = AssistID;

                BcdVolumedirectoryUIRule.UpdateEntity(vm.TheBcdVolumedirectoryInfo);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功",
                    type = "edit",
                    navTabId = "",
                    id = vm.TheBcdVolumedirectoryInfo.Fl_ID,
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 查看未归档文件
        /// </summary>
        /// <returns></returns>
        public ActionResult VolumeDetail(string id)
        {
            BcdFileListViewModel vm = new BcdFileListViewModel();
            //卷内目录信息Volumedirectory
            vm.TheBcdVolumedirectoryInfo = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
            //附件
            var commonFileList = CommonFileUIRule.GetCommonFilesByConditon(vm.TheBcdVolumedirectoryInfo.Assist_ID, "FileList_Rules");//FileList_Rules  BizType类型
            ViewBag.AssisId = vm.TheBcdVolumedirectoryInfo.Assist_ID;
            ViewBag.FileIds = string.Join(",", commonFileList.Select(t => t.FILE_FILEID));
            ViewBag.DeleteIds = string.Join(",", commonFileList.Select(t => t.FILE_ID));
            ViewBag.FilePath = FilePath(vm.TheBcdVolumedirectoryInfo.Vd_ID, vm.TheBcdVolumedirectoryInfo.P_ID, vm.TheBcdVolumedirectoryInfo.Fd_ID);
            return View(vm);
        }
        /// <summary>
        /// 删除未归档文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult VolumeDel(string id)
        {
            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                BcdVolumedirectoryInfo info = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
                BcdVolumedirectoryUIRule.DeleteEntity(id);
                List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + info.P_ID + "' and Fd_ID='" + info.Fd_ID + "' ");
                if (pfList.Count > 0)
                    pInfo = pfList[0];
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 审核未归档文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult VolumeCheck(string id)
        {

            BcdProjectfileInfo pInfo = new BcdProjectfileInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                BcdVolumedirectoryInfo info = BcdVolumedirectoryUIRule.GetEntityById(new Guid(id));
                info.Vd_State = 1;
                info.Vd_CMan = GetLoginUserRealName;
                info.Vd_CTime = DateTime.Now;
                BcdVolumedirectoryUIRule.CheckObject(info);

                List<BcdProjectfileInfo> pfList = BcdProjectfileUIRule.GetEntityListBySql(" P_ID='" + info.P_ID + "' and Fd_ID='" + info.Fd_ID + "' ");
                if (pfList.Count > 0)
                    pInfo = pfList[0];
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "审核成功",
                    type = "check",
                    proId = pInfo.P_ID,
                    pId = "null",
                    id = pInfo.P_ID + pInfo.Fd_ID,
                    name = GetNodeName(pInfo.Fd_Name, pInfo.P_ID, pInfo.Fd_ID),
                    navTabId = "CompletionMaterials.FileData.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "审核失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion
        #endregion
    }
}
